package com.xauto.pioneer.appuser.activity.mine.receipt;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.ReceiptHistoryAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.ReceiptHistoryInfo;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**开票历史列表
 * Created by Administrator on 2018/5/25.
 */

public class ReceiptHistoryActivity extends BaseActivity {
    @Bind(R.id.topBar_receipthistory)
    TopBar topBar_receipthistory;
    @Bind(R.id.list_receipt_history)
    PullToRefreshListView pullToRefreshListView;
    ListView list;
    ReceiptHistoryAdapter adapter;
    private List<ReceiptHistoryInfo> receiptHistoryInfoList;
    ReceiptHistoryInfo historyInfo;

    @Override
    protected int getContentView() {
        return R.layout.ac_receipt_history;
    }

    @Override
    protected void initView() {
        topBar_receipthistory.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar_receipthistory.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
//        topBar_receipthistory.setImage(TopBar.LEFT_IMAGE, R.mipmap.ic_common_left);
        topBar_receipthistory.setText(TopBar.TITLE, "开票历史");
        topBar_receipthistory.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        list = pullToRefreshListView.getRefreshableView();
        list.setVerticalScrollBarEnabled(false);
        list.setDividerHeight(3);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("tag","id = "+id+"--"+position);
//                startActivity(new Intent(ReceiptHistoryActivity.this, MonthlyPayActivity.class));
//                //当前点击订单
//                ParkingOrderInfo currentOrderInfo =  adapter.getItem(position-1);
//                OrderDetailActivity.startAction(mContext,currentOrderInfo.car_order_no);
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //刷新
                page = 1;
                getInvoicedList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    getInvoicedList();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                }
            }
        });
    }

    @Override
    protected void initData() {
        getInvoicedList();
        adapter = new ReceiptHistoryAdapter(this, R.layout.item_receipt_history);
        list.setAdapter(adapter);
        page = 1;
        receiptHistoryInfoList = new ArrayList<>();
        adapter.getDataList().addAll(receiptHistoryInfoList);
        adapter.notifyDataSetChanged();
    }
    private void refreshData(){
        adapter.getDataList().addAll(receiptHistoryInfoList);
        adapter.notifyDataSetChanged();
    }
    /**
     * 显示无数据页面
     */
    private void showNoDataView(){
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_listview_empty, null);
        ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("暂无订单");
        ((ViewGroup)list.getParent()).addView(emptyView);
        list.setEmptyView(emptyView);
    }
    private  void getInvoicedList(){
        new LHttpLib().getInvoicedList(mContext,memberId,new BaseLHttpHandler(mContext,true,true){
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                JSONArray jsonArray = jsonStatus.data.optJSONArray("Invoice_List");
                if(jsonArray!=null&&jsonArray.length()>0){
                    for (int i = 0;i<jsonArray.length();i++){
                        historyInfo = new ReceiptHistoryInfo().parse(jsonArray.optJSONObject(i));
                        receiptHistoryInfoList.add(historyInfo);
                    }
                }else{
                    showNoDataView();
                }
                refreshData();
            }
        });
    }
}
